
import 'package:reabampos/src/reabam/operation/db/models.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/utils/brean/c_bean_good_item.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/// 业务相关的一些常用方法
abstract class RBBusinessUtils {


  //获得挂单流水号
  static int getSerNo(){
    String today=XDateUtils.getCurrentTime_String(format: XDateUtils.formatTimeDefault3);
    String noteDay=XSharePreferencesUtils.getString("XPendingOrderDate${XSharePreferencesUtils.getString(RBConfigs.SP_companyId)}");
    int SerNo=1;
    if(XStringUtils.isEmpty(noteDay)){
      XSharePreferencesUtils.saveString("XPendingOrderDate${XSharePreferencesUtils.getString(RBConfigs.SP_companyId)}",today);
    }else{
      if (today==noteDay){
        int noteInex=XSharePreferencesUtils.getInt("XPendingOrderSerNo${XSharePreferencesUtils.getString(RBConfigs.SP_companyId)}");
        SerNo=noteInex+1;
      }else{
        XSharePreferencesUtils.saveString("XPendingOrderDate${XSharePreferencesUtils.getString(RBConfigs.SP_companyId)}",today);
      }
    }

    XSharePreferencesUtils.saveInt("XPendingOrderSerNo${XSharePreferencesUtils.getString(RBConfigs.SP_companyId)}",SerNo);
    return SerNo;
  }


  static ComponentBeanGoodItem dbSpecBeanToComponentBean(BeanShoppingCartDBGoodSpec bean) {
    ComponentBeanGoodItem item = ComponentBeanGoodItem.fromJson(bean.toJson());
    // item.jsonData = XJsonUtils.json2Map(bean.jsonData);

    //单位
    if (!XStringUtils.isEmpty(bean.unitListJson)) {
      List<dynamic> unitList = XJsonUtils.json2List(bean.unitListJson);
      if (unitList.length != 0) {
        item.unitList = [];
        for (dynamic u in unitList) {
          item.unitList!.add(ComponentBeanUnit.fromJson(u));
        }
      }
    }

    //唯一码
    if (!XStringUtils.isEmpty(bean.uniqueCodeListJson)) {
      List<dynamic> uniqueCodeList = XJsonUtils.json2List(bean.uniqueCodeListJson);
      if (uniqueCodeList.length != 0) {
        item.currentUniqueCodeList = [];
        for (dynamic u in uniqueCodeList) {
          item.currentUniqueCodeList!.add(ComponentBeanUniqueCode.fromJson(u));
        }
      }
    }
    //批次码
    if (!XStringUtils.isEmpty(bean.batchCodeListJson)) {
      List<dynamic> batchCodeList = XJsonUtils.json2List(bean.batchCodeListJson);
      if (batchCodeList.length != 0) {
        item.currentBatchCodeList = [];
        for (dynamic u in batchCodeList) {
          item.currentBatchCodeList!.add(ComponentBeanBatchCode.fromJson(u));
        }
      }
    }

    //店内加工单
    if (!XStringUtils.isEmpty(bean.adjustTypeListJson)) {
      List<dynamic> adjustTypeList = XJsonUtils.json2List(bean.adjustTypeListJson);
      if (adjustTypeList.length != 0) {
        item.adjustTypeList = [];
        for (dynamic u in adjustTypeList) {
          item.adjustTypeList!.add(u);
        }
      }
    }

    return item;
  }

  static ComponentBeanGoodItem dbBeanToComponentBean(BeanShoppingCartDBGood bean) {
    ComponentBeanGoodItem item = ComponentBeanGoodItem.fromJson(bean.toJson());
    // item.jsonData = XJsonUtils.json2Map(bean.jsonData);
    //单位
    if (!XStringUtils.isEmpty(bean.unitListJson)) {
      List<dynamic> unitList = XJsonUtils.json2List(bean.unitListJson);
      if (unitList.length != 0) {
        item.unitList = [];
        for (dynamic u in unitList) {
          item.unitList!.add(ComponentBeanUnit.fromJson(u));
        }
      }
    }

    //唯一码
    if (!XStringUtils.isEmpty(bean.uniqueCodeListJson)) {
      List<dynamic> uniqueCodeList = XJsonUtils.json2List(bean.uniqueCodeListJson);
      if (uniqueCodeList.length != 0) {
        item.currentUniqueCodeList = [];
        for (dynamic u in uniqueCodeList) {
          item.currentUniqueCodeList!.add(ComponentBeanUniqueCode.fromJson(u));
        }
      }
    }
    //批次码
    if (!XStringUtils.isEmpty(bean.batchCodeListJson)) {
      List<dynamic> batchCodeList = XJsonUtils.json2List(bean.batchCodeListJson);
      if (batchCodeList.length != 0) {
        item.currentBatchCodeList = [];
        for (dynamic u in batchCodeList) {
          item.currentBatchCodeList!.add(ComponentBeanBatchCode.fromJson(u));
        }
      }
    }

    //店内加工单
    if (!XStringUtils.isEmpty(bean.adjustTypeListJson)) {
      List<dynamic> adjustTypeList = XJsonUtils.json2List(bean.adjustTypeListJson);
      if (adjustTypeList.length != 0) {
        item.adjustTypeList = [];
        for (dynamic u in adjustTypeList) {
          item.adjustTypeList!.add(u);
        }
      }
    }

    return item;
  }


  //vo转dao
  static BeanShoppingCartDBGood componentBeanToDBBean(
      {required ComponentBeanGoodItem bean, required String xTag, double? quantity}) {
    if (quantity != null) {
      bean.currentQuantity = quantity;
    }

    BeanShoppingCartDBGood dbItem = BeanShoppingCartDBGood.fromJson(bean.toJson());
    dbItem.xTag = xTag;

    return dbItem;
  }

  static BeanShoppingCartDBGoodSpec componentBeanToDBBeanSpec(
      {required ComponentBeanGoodItem bean, required String xTag, double? quantity}) {
    if (quantity != null) {
      bean.currentQuantity = quantity;
    }
    BeanShoppingCartDBGoodSpec dbItem = BeanShoppingCartDBGoodSpec.fromJson(bean.toJson());
    dbItem.xTag = xTag;
    return dbItem;
  }


  //是否多规格
  static bool isSpecType(dynamic item_specType) {
    if (item_specType is ComponentBeanGoodItem) {
      ComponentBeanGoodItem item = item_specType;
      return item.specType != null && '${item.specType}' != '0';
    }

    return item_specType != null && '$item_specType' != '0';
  }

}
